<?php

namespace app\common\model;

require_once ROOT_PATH.'extend/jd_open_api/jd/JdClient.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenGoodsQueryRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/domain/UnionOpenGoodsQuery/GoodsReqDTO.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenCategoryGoodsGetRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenGoodsMaterialQueryRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/domain/UnionOpenGoodsMaterialQuery/GoodsReq.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenGoodsBigfieldQueryRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenCouponQueryRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenPromotionCommonGetRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenPromotionBysubunionidGetRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenOrderRowQueryRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenOrderBonusQueryRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenActivityQueryRequest.php';
require_once ROOT_PATH.'extend/jd_open_api/jd/request/UnionOpenGoodsItemidGetRequest.php';


class Jd
{

    protected $key = 'c4cee6b2d22065716dc21982af46451099d5fd6bf35a932df194b255d8b4b4b10109eb5c4b8454f4';

    protected $url = 'https://api.jd.com/routerjson';

    protected $id = '2034930634';

    protected $appKey = '6e5bc5bd51cd81fc3e9bfa0da3c0833d';

    protected $appSecret = 'b7a1b31179634fee99be13b9b6b44a8f';

    //关键词查询商品
    public function searchGoods($page,$keyword,$cid1,$cid2='',$cid3='',$owner='')
    {
        $c = new \JdClient();
        $c->appKey = $this->appKey;
        $c->appSecret = $this->appSecret;
        //$c->accessToken = accessToken;
        //$c->serverUrl = SERVER_URL;
        $req = new \UnionOpenGoodsQueryRequest();
        //$goodsReqDTO= new GoodsReqDTO();
        $goodsReqDTO = [
            'pageIndex'=>$page,
            //'sceneId'=>2,
            //'spuId'=>'10021803632947',
            //'skuIds'=>[100143427107]
            //'owner'=>'g'
        ];
        if ($keyword)$goodsReqDTO['keyword'] = $keyword;
        if ($cid1)$goodsReqDTO['cid1'] = $cid1;
        if ($cid2)$goodsReqDTO['cid2'] = $cid2;
        if ($cid3)$goodsReqDTO['cid3'] = $cid3;
        if ($owner)$goodsReqDTO['owner'] = 'g';
        //dump($goodsReqDTO);die;
        $req->setGoodsReqDTO($goodsReqDTO);
        //$req->setVersion("1.0");
        $resp = $c->execute($req, $c->accessToken);
        //dump($resp);die();
        if (isset($resp->error_response)){
            throw new \Exception($resp->error_response->zh_desc);
        } else {
            //$resp = json_decode($resp,true);

            $result = json_decode($resp->jd_union_open_goods_query_responce->queryResult,true);

            if ($result['code']==200){
                return $result;
            } else {
                throw new \Exception($result['message']);
            }
        }

    }


    //商品类目查询接口
    public function categoryGoods($parentId=0,$grade=0)
    {
        $c = new \JdClient();
        $c->appKey = $this->appKey;
        $c->appSecret = $this->appSecret;
        $req = new \UnionOpenCategoryGoodsGetRequest();
        $params = [
            'parentId'=>$parentId,
            'grade'=>$grade
        ];
        $req->setReq($params);
        $req->setVersion("1.0");
        $resp = $c->execute($req, $c->accessToken);
        $result = json_decode($resp->jd_union_open_category_goods_get_responce->getResult,true);
        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }



    //网站/APP/流量媒体获取url
    public function getUrl($materialId)
    {
        $c = new \JdClient();
        $c->appKey = $this->appKey;
        $c->appSecret = $this->appSecret;

        $req = new \UnionOpenPromotionCommonGetRequest();
        //$promotionCodeReq= new PromotionCodeReq;
        $promotionCodeReq = [
            'materialId'=>$materialId,
            //'siteId'=>'4101285673',
            'sceneId'=>1
        ];

        $req->setPromotionCodeReq($promotionCodeReq);
        $req->setVersion("1.0");
        $resp = $c->execute($req, $c->accessToken);

        $result = json_decode($resp->jd_union_open_promotion_common_get_responce->getResult,true);
        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //小程序url
    public function getWeappUrl($materialId,$user_id,$couponUrl)
    {
        //$user = User::get($user_id);

        $c = new \JdClient();
        $c->appKey = $this->appKey;
        $c->appSecret = $this->appSecret;

        $req = new \UnionOpenPromotionBysubunionidGetRequest();
        //$promotionCodeReq= new PromotionCodeReq;
        $promotionCodeReq = [
            'materialId'=>$materialId,
            //'subUnionId'=>$user->mobile.'_'.$user_id,
            'couponUrl'=>$couponUrl
            //'sceneId'=>1
        ];

        $req->setPromotionCodeReq($promotionCodeReq);
        $resp = $c->execute($req, $c->accessToken);

        $result = json_decode($resp->jd_union_open_promotion_bysubunionid_get_responce->getResult,true);
        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }


    public function getItemid()
    {
        $c = new \JdClient();
        $c->appKey = $this->appKey;
        $c->appSecret = $this->appSecret;


        $req = new \UnionOpenGoodsItemidGetRequest();
        $itemIdReq= [
            '100082973289'
        ];
        $req->setItemIdReq($itemIdReq);
        $req->setVersion("1.0");
        $resp = $c->execute($req, $c->accessToken);

        if (isset($resp->error_response)){

            throw new \Exception($resp->error_response->zh_desc);
        } else {
            //$resp = json_decode($resp,true);

            $result = json_decode($resp->jd_union_open_goods_query_responce->queryResult,true);

            if ($result['code']==200){
                return $result;
            } else {
                throw new \Exception($result['message']);
            }
        }
    }
}